﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;
using System.Linq;

namespace ConfExpert.Forms.Destex.Satis
{
    public partial class YeniSatisOnay : XPopupForm
    {

        private DataTable dtData;

        public YeniSatisOnay()
        {
            InitializeComponent();
        }

        public YeniSatisOnay(DataTable dtData)
        {
            InitializeComponent();
            this.dtData = dtData;
        }

        private void YeniSatisOnay_Load(object sender, EventArgs e)
        {
            gleMusteri.Edit.Assign(RI(Rp.DX_Musteri));
            DataTable dtSatisLines = dtData.GetChanges();

            var query = dtSatisLines.AsEnumerable()
                        .GroupBy(row => new { Product = row.Field<string>("Product") });

            DataTable dtArtikel = new DataTable();
            dtArtikel.Columns.Add("Product", typeof(string));
            dtArtikel.Columns.Add("ToplamMetraj", typeof(decimal));
            dtArtikel.Columns.Add("ToplamTutarTl", typeof(decimal));

            foreach (var x in query)
            {
                string name = x.Key.Product;
                decimal sumQty = x.Sum(r => r.Field<decimal>("QtyNew"));
                decimal? sumTutar = x.Sum(r => r.Field<decimal?>("ToplamTutarTl"));
                dtArtikel.Rows.Add(x.Key.Product, sumQty, sumTutar);
            }

            DataTable table = ExecuteDataTable("GetDX_SiparisTipi");
            gleSatisTipi.Edit.Assign(GetCustomGLE(table, "SiparisTipiAdi", "SiparisTipiId"));
            
            gleSatisTipi.EditValueChanged += gleSatisTipi_EditValueChanged;

            gv.Init(dtArtikel);
            gv.MakeReadOnly();
            gv.FormatAsNumber(2, "ToplamMetraj", "ToplamTutarTl");
            gv.AddSummary("ToplamMetraj", "ToplamTutarTl");
            gv.BestFitColumns();
        }

        void gleSatisTipi_EditValueChanged(object sender, EventArgs e)
        {
        }

        public int SatisId;
        public int FaturaId;

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gleMusteri.EditValue.ToInt() == 0)
            {
                XtraMessageBox.Show("Müşteri alanı boş bırakılamaz!", "Hata", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return;
            }

            if (gleSatisTipi.EditValue.ToInt() == 0)
            {
                XtraMessageBox.Show("Satış Tipi alanı boş bırakılamaz!", "Hata", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return;
            }
            
            gv.EndEditing();
            DataTable dtChanges = dtData.GetChanges();
            dtChanges.TableName = "DX_Satis";
            string changes = dtChanges.DefaultView.ToTable(false, "ProductId", "Product", "QtyNew", "Fiyat", "CurrencyId", "FiyatTl").Serialize();

            DataTable dtResult = ExecuteDataTable("DX_InsSatis3", changes, gleMusteri.EditValue.ToInt(), gleSatisTipi.EditValue.ToInt(), WhoAmI);
            SatisId = dtResult.Rows[0]["SatisId"].ToInt();
            FaturaId = dtResult.Rows[0]["FaturaId"].ToInt();
            ShowInfoMessage(SatisId + " no'lu satış olarak başarıyla kaydedildi.");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
